//
// CleanerScoreScene class
//
var MainLayer = function () {
    cc.log("MainLayer");
    /**
     * 控制器
     * @type {LevelConsole}
     */
    this.levelControl = null;
    /**
     * 图片的layer
     * @type {cc.Layer}
     */
    this.picLayer = null;
    /**
     * 画线的Layer
     * @type {cc.Layer}
     */
    this.lineLayer = null;
    /**
     * 倒计时
     * @type {cc.ProgressTimer}
     */
    this.timeProgress = null;
    /**
     * 重排的提示
     * @type {cc.Sprite}
     */
    this.reSortSp = null;

    //////////////////////   和布局界面相关的   ////////////////////////////
    this.bar = this.bar || {};
    this.girl = this.girl || {};
    this.musicBtn = this.musicBtn || {};
    this.hintBtn = this.hintBtn || {};
    this.pauseBtn = this.pauseBtn || {};
    this.backBtn = this.backBtn || {};
    this.timebg = this.timebg || {};

};

MainLayer.prototype.onDidLoadFromCCB = function () {
    if (sys.platform == 'browser') {
        this.onEnter();
    }
    else {
        this.rootNode.onEnter = function () {
            this.controller.onEnter();
        };
    }

    this.rootNode.onExit = function () {
        this.controller.onExit();
    };
};

MainLayer.prototype.onEnter = function () {
    //布局
    var size = cc.Director.getInstance().getWinSize();
    this.bar.setPosition(cc.p(size.width - 25, size.height - 25));
    //女孩
    var px = size.width - 880;
    px = Math.min(px, this.girl.getContentSize().width);
    this.girl.setPositionX(px);

    //time
    this.timeProgress = cc.ProgressTimer.create(cc.Sprite.createWithSpriteFrameName("time_front.png"));
    this.timeProgress.setType(cc.PROGRESS_TIMER_TYPE_BAR);
    this.bar.addChild(this.timeProgress);
    this.timeProgress.setPosition(this.timebg.getPosition());

    this.startLevel(1, 0);

    //bind event
    cc.NotificationCenter.getInstance().addObserver(this,this.showReSort,EVENT_RESORTING);
    cc.NotificationCenter.getInstance().addObserver(this,this.hideReSort,EVENT_RESOR_COMPLETE);
    cc.NotificationCenter.getInstance().addObserver(this,this.pause,EVENT_TIME_PAUSE);
    cc.NotificationCenter.getInstance().addObserver(this,this.resume,EVENT_TIME_RESUME);
}

MainLayer.prototype.showReSort = function () {
    this.reSortSp = cc.Sprite.createWithSpriteFrameName("");
}
MainLayer.prototype.hideReSort = function () {

}
MainLayer.prototype.pause = function () {

}
MainLayer.prototype.resume = function () {

}
MainLayer.prototype.onUpdate = function () {

}

MainLayer.prototype.onExitClicked = function () {
    cc.log("onExitClicked");
}


MainLayer.prototype.onExit = function () {
    cc.log("onExit");
}
/**
 * 设置等级和分数，开始关卡
 * @param lv
 * @param score
 */
MainLayer.prototype.startLevel = function (lv, score) {
    var size = cc.Director.getInstance().getWinSize();
    SoundManager.playMusic(SoundManager.PATH_BGSOUND);
    Data.score = score;
    Data.currentLevel = lv;
    Data.currentHint = Data.currentLevel == 1 ? Data.hintNumbers : Data.currentHint;
    if (!this.levelControl) {
        this.picLayer = cc.Node.create();
        this.rootNode.addChild(this.picLayer, 10);
        this.picLayer.setPosition(cc.p(size.width - 880, 30));
        this.lineLayer = cc.DrawNode.create();
        this.rootNode.addChild(this.lineLayer, 20);
        this.lineLayer.setPosition(this.picLayer.getPosition());
        this.levelControl = new LevelConsole(this.lineLayer, this.picLayer);
    }
    this.levelControl.reset(Data.currentLevel);
}
/**
 * 控制音乐播放的
 */
MainLayer.prototype.onMusicClick = function () {
    SoundManager.setHasMusic(!SoundManager.isAudio);
    var cache = cc.SpriteFrameCache.getInstance();
    if (SoundManager.isAudio) {
        this.musicBtn.setNormalSpriteFrame(cache.getSpriteFrame("music.png"));
        this.musicBtn.setSelectedSpriteFrame(cache.getSpriteFrame("music_press.png"));
    } else {
        this.musicBtn.setNormalSpriteFrame(cache.getSpriteFrame("nomusic.png"));
        this.musicBtn.setSelectedSpriteFrame(cache.getSpriteFrame("nomusic_press.png"));
    }
}
/**
 * 点击提示
 */
MainLayer.prototype.onHintClick = function () {
    trace("hint me !!!");
}
/**
 * 点击暂停
 */
MainLayer.prototype.onPauseClick = function () {
    trace("pause !!!");
}
/**
 * 点击返回
 */
MainLayer.prototype.onBackClick = function () {
    var size = cc.Director.getInstance().getWinSize();
    var panel = cc.BuilderReader.loadAsNodeFrom("", "PausePanel");
    panel.type(PausePanel.TYPE_BACK);
    this.rootNode.addChild(panel, 100);
    this.canClick = false;
}

